package org.iabako.server.controller.timer.logtrack;

import org.apache.log4j.Logger;
import org.iabako.server.dao.user.EnterpriseDAO;
import org.iabako.server.dao.user.UserDAO;
import org.iabako.server.tools.ServerTools;
import org.iabako.shared.entity.user.Enterprise;
import org.iabako.shared.entity.user.User;
import org.iabako.shared.tools.GenericTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;

/**
 * Created by jose on 18/05/15.
 */

@Controller
public class ProspectsLogTrackController {

    static Logger log = Logger.getLogger(ProspectsLogTrackController.class.getName());

    @Autowired
    private UserDAO userDAO;
    @Autowired
    private EnterpriseDAO enterpriseDAO;

    public String trackProspectsLogs() {
        log.info("Executing ProspectsLogTrackController...");

        ServerTools serverTools = new ServerTools();
        Date yesterday = serverTools.addDaysToDate(serverTools.resetTime(new Date()), -1);

        StringBuilder usersResult = new StringBuilder();
        StringBuilder enterprisesResult = new StringBuilder();
        StringBuilder result = new StringBuilder();

        List<User> users = userDAO.getDemoUserCreatedOn(yesterday);
        for (User user : users) {
            usersResult.append(GenericTools.formatTime(user.getCreationDate())
                    + " : " + user.getEmail()
                    + " : " + user.isEmailConfirmed() + "\n");
        }

        List<Enterprise> enterprises = enterpriseDAO.getEnterprisesCreatedOn(yesterday);
        for (Enterprise enterprise : enterprises) {
            enterprisesResult.append(GenericTools.formatTime(enterprise.getCreationDate())
                    + " : " + enterprise.getAddress().getCountry().getPaypalCode()
                    + " : " + enterprise.getName());
            List<User> usersList = enterprise.getUsers();
            if (!usersList.isEmpty()) {
                User user = usersList.get(0);
                enterprisesResult.append(" : " + user.getEmail()
                        + " : " + user.getName()
                        + " : " + user.isEmailConfirmed()
                        + " : " + (usersList.get(0).isActive() ? "active" : "disable"));
            }
            enterprisesResult.append("\n");
        }

        if (usersResult.length() > 0 || enterprisesResult.length() > 0) {

            result.append("Dear Admin,\nThis is the prospects tracking log of " + GenericTools.formatDate(yesterday) + " : \n");

            if (usersResult.length() > 0) {
                result.append("\n<b>DEMO Visitors : </b>\n");
                result.append("DATE : EMAIL : CONFIRMED\n");
                result.append(usersResult.toString());
            }

            if (enterprisesResult.length() > 0) {
                result.append("\n<b>New Enterprises : </b>\n");
                result.append("DATE : COUNTRY : NAME : USER : EMAIL : CONFIRMED : STATUS\n");
                result.append(enterprisesResult.toString());
            }
        }

        return result.toString();
    }

}
